package com.secret.chain.secretchain.service.impl;

import com.secret.chain.secretchain.domain.entity.Article;

import com.secret.chain.secretchain.domain.entity.Like;
import com.secret.chain.secretchain.mapper.ArticleMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;

import com.secret.chain.secretchain.result.Result;
import com.secret.chain.secretchain.service.ArticleService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@MapperScan("com.secret.chain.secretchain.mapper")
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public Result<Article> queryById(Integer id) {
        Article article = articleMapper.queryById(id);
        return Result.success(article);
    }

    @Override
    public Result deleted(Integer id) {
        articleMapper.deleteById(id);
        return Result.success();
    }

    @Override
    public Result<Void> save(Article article) {
        LocalDateTime now =LocalDateTime.now();
        article.setCreatTime(now);
        article.setStatus(1);
        article.setUpdateTime(null);
        article.setLikeCount(0);
        article.setCollectCount(null);
        article.setForwardCount(null);
        article.setReadCount(null);
        articleMapper.insert(article);
        return Result.success();
    }

    @Override
    public Result<Void> put(Article article) {
        articleMapper.put(article);
        return Result.success();
    }

    @Override
    public Result<Void> add(Like like) {
        LocalDateTime now =LocalDateTime.now();
        like.setCreatTime(now);
        like.setStatus(1);
        like.setUpdateTime(null);
        articleMapper.add(like);
        return Result.success();
    }

}
